<template><div><h1 id="vue-项目上线时如何去掉代码中的console-打印信息" tabindex="-1"><a class="header-anchor" href="#vue-项目上线时如何去掉代码中的console-打印信息" aria-hidden="true">#</a> Vue 项目上线时如何去掉代码中的console 打印信息</h1>
<div class="language-javascript ext-js line-numbers-mode"><pre v-pre class="language-javascript"><code>一、安装插件 babel<span class="token operator">-</span>plugin<span class="token operator">-</span>transform<span class="token operator">-</span>remove<span class="token operator">-</span>console

命令：npm install babel<span class="token operator">-</span>plugin<span class="token operator">-</span>transform<span class="token operator">-</span>remove<span class="token operator">-</span>console <span class="token operator">--</span>save


<span class="token comment">// babel.config.js 修改后：</span>


<span class="token keyword">const</span> prodPlugins <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>process<span class="token punctuation">.</span>env<span class="token punctuation">.</span><span class="token constant">NODE_ENV</span> <span class="token operator">===</span> <span class="token string">'production'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  prodPlugins<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'transform-remove-console'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
 
module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">presets</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token string">'@vue/cli-plugin-babel/preset'</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token literal-property property">plugins</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token operator">...</span>prodPlugins<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


